package net.dntuan.training.mvc.dao.impl;

import java.util.List;

import net.dntuan.training.mvc.dao.CityDao;
import net.dntuan.training.mvc.domain.City;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

//The annotation @Repository enables exception translation
@Repository("cityDao")
public class CityDaoImpl extends BaseDaoImpl<City> implements CityDao {

    public CityDaoImpl() {
        setClazz(City.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<City> search(String cityName, String country, Integer minPop, Integer maxPop) {
        Criteria criteria = getCurrentSession().createCriteria(clazz);
        if (StringUtils.isNotBlank(cityName)) {
            criteria.add(Restrictions.ilike("name", "%" + cityName.trim() + "%"));
        }
        return criteria.list();
    }

}
